#version 460
#define GLSLIFY 1

layout(points) in;
//layout(triangle_strip, max_vertices=128) out;
layout(line_strip, max_vertices=30) out;
//layout(points, max_vertices=24) out;

layout(std140, set=0, binding=0) uniform U_Matrix {
	mat4 mat_ProjecView;	
};

//layout(std140, set=1, binding=1) uniform mat4 Model_Matrix;
layout(std140, binding = 1) readonly buffer U1_Matrix {
	mat4 mat_Model[];
};

layout(std140, column_major, binding = 2) uniform U2_Matrix {
	//体素分辨率
	uvec4	m_Resolution;
	//世界大小
	vec4	m_WorldSize;
	//世界偏移
	vec4	m_WorldOffset;
	//单元大小
};

//====================== 顶点阶段输入数据 =====================
layout(location = 0) in vec4 vColor[];

in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 3) in VS_GS_VERTEX {
	uint	InstanceIndex;
	float	VType;
}vertex_in[];

//======================= 几何阶段输出数据 ========================
/*layout(location = 1) out VS_FS_VERTEX {
	vec4		gColor;
	flat ivec3	vUV_Offset;
	flat ivec2	vUV_Bound;
};*/

void main(void){
	// 位置  大小  对齐方式  深度
	mat4 mat = mat_Model[vertex_in[0].InstanceIndex];
	
	vec4 v = gl_in[0].gl_Position;
	int VType = int(vertex_in[0].VType);

	float m_UnitInterval = m_WorldSize.w;

	
	//间隔步进
	//float advance = 0;
	//-Y;
	if (bool(VType & 1<<4)) {
		vec4 currentPoint = v;
		
		currentPoint.xyz -= m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.x += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.x -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint = v;
		currentPoint.xyz -= m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		EndPrimitive();
	}
	//+Y
	if (bool(VType & 1<<22)) {
		vec4 currentPoint = v;
		
		currentPoint.xyz += m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.x -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint = v;
		currentPoint.xyz += m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		EndPrimitive();
	}
	//-X
	if (bool(VType & 1<<12)) {
		vec4 currentPoint = v;
		
		currentPoint.xyz -= m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.y += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint = v;
		currentPoint.xyz -= m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		EndPrimitive();
	}
	//+X
	if (bool(VType & 1<<14)) {
		vec4 currentPoint = v;
		
		currentPoint.xyz += m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.y -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint = v;
		currentPoint.xyz += m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		EndPrimitive();
	}
	//-Z
	if (bool(VType & 1<<10)) {
		vec4 currentPoint = v;
		
		currentPoint.xyz -= m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.x += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint = v;
		currentPoint.xyz -= m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		EndPrimitive();
	}
	//-Z
	if(bool(VType & 1<<16)){
		vec4 currentPoint = v;
		
		currentPoint.xyz += m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.x -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.z -= m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint.x += m_UnitInterval;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		currentPoint = v;
		currentPoint.xyz += m_UnitInterval * 0.5;
		gl_Position = mat_ProjecView * mat * currentPoint;
		EmitVertex();

		EndPrimitive();
	}

	/*for(int i=0; i<6; ++i){

		//gl_Position = vec4(100, 200, 0, 1.0);
		v.x += i*2;
		gl_Position = mat_ProjecView * mat * v;
		//gl_PointSize = 3.0;
		EmitVertex();
	}

	EndPrimitive();*/
	
}

 